package com.campus.activity.controller;

import com.campus.activity.common.Result;
import com.campus.activity.entity.Registration;
import com.campus.activity.service.RegistrationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 报名管理控制器
 * 
 * @author System
 * @since 2024
 */
@RestController
@RequestMapping("/registrations")
@Tag(name = "报名管理", description = "学生活动报名和报名状态管理")
@Validated
@CrossOrigin(origins = "*")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;

    @PostMapping("/register")
    @Operation(summary = "学生报名活动", description = "学生报名参加指定活动")
    public Result<Boolean> registerActivity(
            @Parameter(description = "学生ID") @RequestParam Integer studentId,
            @Parameter(description = "活动ID") @RequestParam Integer activityId) {
        boolean success = registrationService.registerActivity(studentId, activityId);
        if (success) {
            return Result.success("报名成功", true);
        } else {
            return Result.error("报名失败，可能已报名或活动已满员");
        }
    }

    @DeleteMapping("/{registrationId}")
    @Operation(summary = "取消报名", description = "取消指定的报名记录")
    public Result<Boolean> cancelRegistration(
            @Parameter(description = "报名ID") @PathVariable Integer registrationId) {
        boolean success = registrationService.cancelRegistration(registrationId);
        return success ? Result.success("取消报名成功", true) : Result.error("取消报名失败");
    }

    @DeleteMapping("/cancel")
    @Operation(summary = "取消学生活动报名", description = "取消学生对指定活动的报名")
    public Result<Boolean> cancelRegistrationByStudentAndActivity(
            @Parameter(description = "学生ID") @RequestParam Integer studentId,
            @Parameter(description = "活动ID") @RequestParam Integer activityId) {
        boolean success = registrationService.cancelRegistrationByStudentAndActivity(studentId, activityId);
        return success ? Result.success("取消报名成功", true) : Result.error("取消报名失败");
    }

    @PutMapping("/{registrationId}/status")
    @Operation(summary = "更新报名状态", description = "更新指定报名记录的状态")
    public Result<Boolean> updateRegistrationStatus(
            @Parameter(description = "报名ID") @PathVariable Integer registrationId,
            @Parameter(description = "新状态") @RequestParam String status) {
        boolean success = registrationService.updateRegistrationStatus(registrationId, status);
        return success ? Result.success("状态更新成功", true) : Result.error("状态更新失败");
    }

    @GetMapping("/student/{studentId}")
    @Operation(summary = "获取学生报名状态", description = "获取指定学生的所有报名记录")
    public Result<List<Registration>> getRegistrationStatus(
            @Parameter(description = "学生ID") @PathVariable Integer studentId) {
        List<Registration> registrations = registrationService.getRegistrationStatus(studentId);
        return Result.success(registrations);
    }

    @GetMapping("/activity/{activityId}")
    @Operation(summary = "获取活动报名列表", description = "获取指定活动的所有报名记录")
    public Result<List<Registration>> getRegistrationsByActivity(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        List<Registration> registrations = registrationService.getRegistrationsByActivityId(activityId);
        return Result.success(registrations);
    }

    @GetMapping("/activity/{activityId}/statistics")
    @Operation(summary = "获取活动报名统计", description = "获取指定活动的报名统计信息")
    public Result<Map<String, Object>> getRegistrationStatistics(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        Map<String, Object> statistics = registrationService.getRegistrationStatistics(activityId);
        return Result.success(statistics);
    }

    @GetMapping
    @Operation(summary = "查询报名信息", description = "根据条件查询报名信息")
    public Result<List<Registration>> queryRegistrationInfo(
            @Parameter(description = "活动ID") @RequestParam(required = false) Integer activityId,
            @Parameter(description = "报名状态") @RequestParam(required = false) String status) {
        List<Registration> registrations = registrationService.queryRegistrationInfo(activityId, status);
        return Result.success(registrations);
    }

    @GetMapping("/check")
    @Operation(summary = "检查报名状态", description = "检查学生是否已报名指定活动")
    public Result<Boolean> checkRegistrationStatus(
            @Parameter(description = "学生ID") @RequestParam Integer studentId,
            @Parameter(description = "活动ID") @RequestParam Integer activityId) {
        boolean isRegistered = registrationService.isStudentRegistered(studentId, activityId);
        return Result.success(isRegistered);
    }

    @GetMapping("/activity/{activityId}/count")
    @Operation(summary = "获取活动报名人数", description = "获取指定活动的当前报名人数")
    public Result<Integer> getCurrentRegistrationCount(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        Integer count = registrationService.getCurrentRegistrationCount(activityId);
        return Result.success(count);
    }

    @GetMapping("/activity/{activityId}/approved-count")
    @Operation(summary = "获取活动通过人数", description = "获取指定活动的已通过报名人数")
    public Result<Integer> getApprovedRegistrationCount(
            @Parameter(description = "活动ID") @PathVariable Integer activityId) {
        Integer count = registrationService.getApprovedRegistrationCount(activityId);
        return Result.success(count);
    }
}